<?php
/**
 *
帮助控制器。
 *
 **/

namespace Admin\Controller;

class HelpController extends ComController
{
    public function add()
    {
        $this->display('edit');
    }

    public function index()
    {
        $info = M('help')->order('id desc')->select();
        $this->assign('help',$info);
        $this->display();
    }

    public function edit()
    {
        $help = M('help');
        $where['id'] = I('get.aid');
        $info = $help->where($where)->find();
        $this->assign('help',$info);
        $this->display();
    }

    public function del()
    {

        $aids = isset($_REQUEST['aids']) ? $_REQUEST['aids'] : false;
        if ($aids) {
            if (is_array($aids)) {
                $aids = implode(',', $aids);
                $map['id'] = array('in', $aids);
            } else {
                $map = 'id=' . $aids;
            }
            if (M('help')->where($map)->delete()) {
                addlog('删除帮助，AID：' . $aids);
                $this->success('恭喜，帮助删除成功！',U('index'));
            } else {
                $this->error('参数错误！',U('index'));
            }
        } else {
            $this->error('参数错误！',U('index'));
        }

    }

    public function update()
    {
        $id = I('post.aid');
        $help = M('help');
        $data['title'] = I('post.title');
        $data['content'] = I('post.content');
        if($id){
            $res = $help->where(array('id'=>$id))->save($data);
            addlog('编辑帮助，ID：' . $id);
            $this->success('恭喜！编辑帮助成功！',U('index'));
        }else{
            $res = $help->add($data);
            if ($res) {
                addlog('新增帮助，AID：' . $res);
                $this->success('恭喜！添加帮助成功！',U('index'));
            } else {
                $this->error('抱歉，未知错误！',U('index'));
            }
        }
    }
}